<?php
class ArticleController extends DomController {
	
	public function articles() {
		
		$post = DomFgPost::getInstance('articles')->store('articles', array('search') );
		$query = Article::query()->orderBy('t.publishDateTime DESC');
					
		if( $_GET['delete'] && $item = Article::load( $_GET['delete'] )){
			$item->delete();
		}
		
		if( $post->search ) {
			$query->where('headline LIKE \'%'.addslashes($post->search).'%\' or author LIKE \''.addslashes($post->search).'\'');
		}
				
		$panel = array(
			'title' => 'Artiklid',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 4,
				'columnWidths' => array(150,60,220),
			),
			'style' => 'width:800px',
			'action' => $this->getUrl('articles/'),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Lisa uus',
					'link' => $this->getUrl('article/'),
				),
				array(
					'type' => 'textField',
					'value' => $post->search,
					'name' => 'search',
					'title' => 'Otsi',
				),
				array(
					'type' => 'button',
					'title' => 'Otsi',
					'name' => 'doSearch',
				),
				array(
					'type' => 'grid',
					'name' => 'articles',
					'pagerResetPostVar' => 'doSearch',
					'source' => new DomArDataSource( $query, 40, articles ),
					'colspan' => 4,
					'fields' => array(
						array('key' => 'headline', 'title' => 'Pealkiri'),
						array('key' => 'author', 'title' => 'Autor'),
						array('type' => 'gridButton', 'value' => 'Muuda', 'link' => $this->getUrl('article/'), 'title' => 'Valikud','colspan' => 2, 'width'=>70),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' =>$this->getUrl('articles/&delete='), 'confirmMessage' => 'Are you sure?', 'width'=>70 ),
					),
				),
			),
		);
				
		$this->view->contents = DomFg::render( $panel );
		
	}
	
	public function article( $id = NULL ) {
		$new = $id == 0;
		$item = $new ? new Article() : Article::load($id);
						
		$post = DomFgPost::getInstance();
		
		if( $_GET['deleteImage'] && $im  = ArticleImage::load($_GET['deleteImage']) ) {
			$im->delete();
		}
		
		if( $post->save ) {
			
			$item->headline = $post->headline;
			$item->introduction = $post->introduction;
			$item->contents = $post->contents;
			$item->author = $post->author;
			$item->publishDateTime = $post->publishDateTime;
			$item->category = (int)$post->category;
			$item->image->addUploaded( $post->image[0], $post->image[1] );
			$item->allowComments = (bool)$post->allowComments;
			
			$item->lists->set( $post->lists );
			
			foreach( $item->images->query()->orderBy('t.`order`')->commit() as $i => $image ) {
				$image->order = $post->images['order'][$i];
			}
			
			if( $post->newImage[0] ) {
				$image = new ArticleImage();
				$image->image->addUploaded( $post->newImage[0], $post->newImage[1] );
				$item->images[] = $image;
			}
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array('message' => 'Salvestatud','type' => 'ok','colspan' => 2,);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'error','colspan' => 2,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}
				
		// ---------------------------------------
		
		$tabPanel = array(
			'type' => 'tabPanel',
			'headerTabs' => true,
			'action' => $this->getUrl('article/'.$id),
			'items' => array($message),
			'style' => 'width:800px',
			'tabsStyle' => 'width:800px',
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
			),
		);
				
		// ---------------------------------------
				
		$tab1 = array(
			'type' => 'panel',
			'title' => 'Üldine',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 3,
				'columnWidths' => array( 150,200 ),
			),
			'items' => array(
				array(
					'type' => 'image',
					'src' => $item->icon->getUrl(),
					'rowspan' => 10,
					'style' => 'width:150px; height:150px;'
				),
				array(
					'type' => 'textField',
					'title' => 'Pealkiri',
					'value' => $item->headline,
					'name' => 'headline',
				),
				array(
					'type' => 'textField',
					'title' => 'Autor',
					'value' => $item->author,
					'name' => 'author',
				),
				array(
					'type' => 'dateTime',
					'title' => 'Avalikustatakse',
					'value' => $item->publishDateTime,
					'name' => 'publishDateTime',
				),
				array(
					'type' => 'file',
					'title' => 'Pilt',
					'name' => 'image',
				),
				array(
					'type' => 'select',
					'title' => 'Kategooria',
					'values' => Article::getCategories(),
					'selected' => $item->category,
					'name' => 'category',
				),
				array(
					'type' => 'checkBox',
					'name' => 'allowComments',
					'title' => 'Luba kommenteerimine',
					'checked' => $item->allowComments,
				),
				array(
					'type' => 'multiSelect',
					'name' => 'lists',
					'title' => 'Listid',
					'values' => ArticleList::idSetQuery('name')->orderBy('name')->commit()->toArray(),
					'selected' => $item->lists->idSetQuery('id')->commit()->toArray(),
				),
			),
		);
		$tabPanel['items'][] = &$tab1;
		
		// ---------------------------------------
		
		$tab2 = array(
			'type' => 'panel',
			'title' => 'Sissejuhatus',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 1,
			),
			'items' => array(
				array(
					'type' => 'htmlEditor',
					'value' => $item->introduction,
					'name' => 'introduction',
					'style' => 'height:300px;',
				),
			),
		);
		$tabPanel['items'][] = &$tab2;
		
		// ---------------------------------------
		
		$tab3 = array(
			'type' => 'panel',
			'title' => 'Sisu',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 1,
			),
			'items' => array(
				array(
					'type' => 'htmlEditor',
					'value' => $item->contents,
					'name' => 'contents',
					'style' => 'height:500px;',
				),
			),
		);
		$tabPanel['items'][] = &$tab3;
			
		// ---------------------------------------
		
		$images = $item->images->query()->orderBy('t.`order`');
		
		function image( $row ) {
			return $row->icon->toHtml();
		}
		
		$tab4 = array(
			'id' => 'images',
			'type' => 'panel',
			'title' => 'Galerii',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
			),
			'items' => array(
				array(
					'type' => 'imageGrid',
					'colspan' => 2,
					'id' => 'images',
					'name' => 'images',
					'ordering' => true,
					'source' => new  DomArDataSource( $images,999 ),
					'fields' => array(
						array('handler'=>'image',),
						array('type'=>'gridButton',	'value' => 'Kustuta', 'link'=>$this->getUrl('article/'.$item->id.'/&tab=images&deleteImage='), 'confirmMessage' => 'Oled sa kindel, et soovid kustutada?' ),
					),
				),
				array(
					'type' => 'file',
					'name' => 'newImage',
					'title' => 'Lisa uus pilt',
				),
			),
		);
		$tabPanel['items'][] = &$tab4;
			
		// ---------------------------------------
				
		$this->view->contents = DomFg::render( $tabPanel );
	}

	public function lists() {
		if( $_GET['delete'] && $item = ArticleList::load( $_GET['delete'] )){
			$item->delete();
		}

		$source = new DomArDataSource( ArticleList::query()->orderBy('t.name DESC'), 40 );
		
		$panel = array(
			'title' => 'Listid',
			'layout' => 'table',
			'style' => 'width:500px',
			'layoutConfig' => array(
				'columns' => 1,
			),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Lisa uus',
					'link' => $this->getUrl('editList/'),
				),
				array(
					'type' => 'grid',
					'source' => $source,
					'fields' => array(
						array('key' => 'name', 'title' => 'Nimi'),
						array('type' => 'gridButton', 'value' => 'Muuda', 'link' => $this->getUrl('editList/'), 'title' => 'Valikud','colspan' => 2, 'width'=>70),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' => $this->getUrl('lists/&delete='), 'confirmMessage' => 'Are you sure?', 'width'=>70 ),
					),
				),
			),
		);
					
		$this->view->contents = DomFg::render( $panel );
		
	}
	
	public function editList( $id = NULL ) {
		$new = $id == 0;
		$item = $new ? new ArticleList() : ArticleList::load($id);
				
		$post = DomFgPost::getInstance();
		
		if( $post->save ) {
			
			$item->name = $post->name;
			$item->description = $post->description;
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array('type' => 'ok','colspan' => 2,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'ok','colspan' => 2,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}
				
		$panel = array(
			'title' => 'List',
			'layout' => 'table',
			'action' => $this->getUrl('editList/'.$id),
			'style' => 'width:650px',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				$message ,
				array(
					'type' => 'textField',
					'title' => 'Nimi',
					'value' => $item->name,
					'name' => 'name',
					'style' => 'width:500px',
				),
				array(
					'type' => 'htmlEditor',
					'title' => 'Kirjeldus',
					'value' => $item->description,
					'name' => 'description',
					'style' => 'width:500px',
				),
			),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
			),
		);
				
		$this->view->contents = DomFg::render( $panel );
		
	}
	
	public function setup() {
		
		//Article::getTable()->update();
		
		//$this->auth('articles');
		
		$menu = array(
			'type' => 'menu',
			'items' => array(
				array(
					'title' => 'Artiklid',	
					'link' => $this->getUrl('articles/'),
				),
				array(
					'title' => 'Listid',	
					'link' => $this->getUrl('lists/'),
				),
			),
		);
		$this->view->menu = DomFg::render( $menu );
		
	}
}
?>